package com.example.dreamlist2.goal;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import com.example.dreamlist2.R;
import com.example.dreamlist2.StateInfo;
import com.example.dreamlist2.todo.TodoAdapter;
import com.example.dreamlist2.todo.TodoAddActivity;
import com.example.dreamlist2.todo.TodoEditActivity;
import com.example.dreamlist2.todo.TodoItem;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

public class GoalFragment extends Fragment {
    private boolean isGetData = false;
    protected Activity mActivity;
    //StateInfo stateInfo = (StateInfo)(requireActivity().getApplication());

    ListView listView;
    private List<HabitItem> habitList = new ArrayList<>();
    Button add;

    String uid;

    StateInfo stateInfo;

    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        //   进入当前Fragment
        if (enter && !isGetData) {
            isGetData = true;
            //   这里可以做网络请求或者需要的数据刷新操作
            Bundle bundle = getArguments();
            assert bundle != null;
            uid = bundle.getString("uid");
            //展示所有待办事项
            search_habit(uid);
        } else {
            isGetData = false;
        }
        return super.onCreateAnimation(transit, enter, nextAnim);
    }

    @Override
    public void onPause() {
        super.onPause();
        isGetData = false;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
    @Override
    public void onAttach(@NotNull Context context) {
        this.mActivity = (Activity)context;
        super.onAttach(context);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.habit_main, container, false);
        listView = view.findViewById(R.id.habit_listview);
        add = view.findViewById(R.id.habit_add);
        stateInfo = (StateInfo) mActivity.getApplication();

        Bundle bundle = getArguments();
        assert bundle != null;
        uid = bundle.getString("uid");

        //展示所有打卡事项
        search_habit(uid);

        //增加打卡事项
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mActivity, HabitAddActivity.class);
                //request:1 result:1
                startActivityForResult(intent, 1);
            }
        });

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                HabitItem habitItem = habitList.get(position);
                stateInfo.setHabitin(habitItem);
                Intent intent = new Intent(mActivity, HabitCompleteActivity.class);
//                Bundle bundle2 = new Bundle();
                intent.putExtra("item", habitItem);
                //request:2 result:2
                startActivityForResult(intent, 2);
            }
        });
        return view;
    }

    public void search_habit(String uid){
        BmobQuery<HabitItem> query= new BmobQuery<HabitItem>();
        query.addWhereEqualTo("uid", uid);
        query.findObjects(new FindListener<HabitItem>() {
            @Override
            public void done(List<HabitItem> list, BmobException e) {
                if (e == null) {
                    //Toast.makeText(mActivity, "成功,共" + list.size() + "条数据", Toast.LENGTH_LONG).show();
                    Log.i("habit","查询成功"+uid+" "+list.size());
                    habitList = list;
                    listView.setAdapter(new HabitAdapter(mActivity, habitList));
                }
                else {
                    Log.i("habit","查询不成功");
                }
            }
        });


    }
}